<template>
  <div class="frame">
    <el-dialog
      :title=title
      :visible.sync="showIn"
      width="440px"
      :before-close="closeFrame"
    >
      <div  class="password">
        <div>
          <p>{{$t("ac.notice")}}</p>
          <span>{{$t("ac.notice1")}}</span><br>
          <span>{{$t("ac.notice2")}}</span><br>
          <span>{{$t("ac.notice3")}}</span>
        </div>
        <p>{{$t("ac.password")}}</p>
        <input type="password"  :placeholder="this.$t('ac.passwordPlaceholder')" v-model="secret">
        <button @click="confirm">{{$t("ac.sumbit")}}</button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
    import  {sellram,buyram,undelegatebw,selectOuterDatx} from  '../../service/datxService'
  import {validateSecret,getforNumber} from '../../utils/commonUtils'
  export default{
    data(){
      return{
        isRam:false,
        isCpuNet:false,
        ifpassword:false,
        datxDetail:{},
        showIn:true,
        coinName:'',
        account:'', //账号
        title: '赎回资源',
        buyer:'',  // 购买者
        receiver:'',// 接收者
        secret:''//密码
      }
    },
    props: ['bytes','unstake_net_quantity','unstake_cpu_quantity'],
    created(){
      this.getMessage()
    },
    methods:{
      getMessage(){
        this.datxDetail = JSON.parse(window.sessionStorage.getItem("walletdetail"));
        this.account=this.datxDetail.name;
        this.buyer=this.datxDetail.name;
        this.receiver=this.datxDetail.name;
        this.form=this.datxDetail.name;
      },
      closeFrame(){
        this.$emit('redeeListen',false)
      },
      // 确认密码
      confirm(){
        console.log("内存参数",this.buyer,this.receiver,this.bytes+" DATX" ,"解除质押参数",this.from,this.receiver,this.unstake_net_quantity+" DATX",this.unstake_cpu_quantity+" DATX",1,)
        if(validateSecret(this.secret)){
          let obj=selectOuterDatx(this.datxDetail.name,this.secret)
          let privateKey=obj.privateKey
          if(privateKey){
            if(this.bytes>0){
              sellram(this.account, Math.round(this.bytes),privateKey,(erro,data) =>{
                console.log(erro,data)
                if(erro){
                  this.$message.error('erro')
                }else {
                  this.$message.success("success")
                }
              })
            }
              if(this.unstake_net_quantity>0||this.unstake_cpu_quantity>0){
                undelegatebw(this.form,this.receiver,getforNumber(this.unstake_net_quantity)+" DATX",getforNumber(this.unstake_cpu_quantity)+" DATX",privateKey,(erro,data) =>{
                  console.log(erro,data)
                  if(erro){
                    this.$message.error('erro')
                  }else {
                    this.$message.success("success")
                  }
                })
              }
          }else {
            this.$message.error(this.$t('dt.privatekeyNoExist'))
          }
        }else {
          this.$message.error(this.$t('dt.passwordDefaule'))
        }
        this.$emit('redeeListen',false)
      }
    }
  }
</script>
<style lang="less" scoped>
  .frame{
    .content{
      .content-input{
        padding: 10px 20px;
        margin-top: 20px;
        background:#fbfbfc;;
        color: #666666;
        input{
          width: 349px;
          height: 34px;
          padding: 0 10px;
          border-radius: 4px;
          border: solid 1px #eeeeee;
          background-color: #ffffff;
        }
        input::-webkit-input-placeholder {
          font-size: 12px;
          color: #c5c7cb;
        }
        .balance{
          margin-bottom: 15px;
        }
        .transfer{
          padding-bottom: 20px;
          border-bottom: 1px solid #eaedf5;
          div{
            line-height: 34px;
          }
        }
        .memo{
          padding-top: 20px
        }
        .adress{
          margin-bottom: 15px;
        }

      }
      .content-button{
        margin: 22px 0;
        button{
          color:#fff;
          width: 400px;
          margin:  0 auto;
          display: block;
          border-radius: 4px;
          line-height:32px;
          text-align: center;
          background-color: #98889b;
          outline: none;
          border: none;
          cursor: pointer;
        }
        button:hover{
          opacity: .8;
        }
      }
    }
    .password{
      padding: 0 20px;
      p{
        color: #666666;
        padding:15px 0;
      }
      input{
        width: 100%;
        height: 34px;
        padding: 0 10px;
        border-radius: 4px;
        border: solid 1px #eeeeee;
        background-color: #ffffff;
      }
      input::-webkit-input-placeholder {
        font-size: 12px;
        color: #c5c7cb;
      }
      button{
        width: 100%;
        margin: 30px 0 10px;
        color: #fff;
        border-radius: 4px;
        text-align: center;
        line-height: 40px;
        background: #98889b;
        outline: none;
        cursor: pointer;
      }
      button:hover{
        opacity: .8;
      }
      div{
        margin: 0px;
        p{
          padding: 5px 0;
          text-align: center;
        }
        span{
          font-size: 12px;
          color: #98889b;
        }
      }
    }
  }
</style>
<style lang="less" >
  .frame{
    .el-dialog__wrapper{
      .el-dialog{
        border-radius: 8px;
      }
      .el-dialog__header{
        border-bottom:1px solid #eaedf5;
        color:#000000;
        font-size: 18px;
        font-weight: bold;
        padding:20px;
      }
      .el-dialog__body{
        padding:30px 0;
      }
      .el-dialog__headerbtn .el-dialog__close{
        color: #666666;
      }
    }
  }
</style>


